<?php
// +----------------------------------------------------------------------
// | 地址
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------
	namespace app\controller\api\v3;

    use yicmf\tools\HttpService;

	class Statistics extends Common
	{


        /**
         * 客户统计
         * @return mixed
         */
        public function sum(ChatUserServices $services)
        {
            return $this->success($services->getKefuSum($this->kefuInfo['appid']));
        }

        /**
         * 客户首页统计
         * @return mixed
         */
        public function index()
        {


            $data['code'] = 0;
            $data['data']['categories'] = ["2018","2019","2020","2021","2022","2023"];//全部客户
            $data['data']['series'][] = ['name'=>'客户','data'=>[2,2,3,4,5,6,7,8]];//全部客户
            $data['data']['series'][] = ['name'=>'游客','data'=>[3,2,3,4,5,6,7,8]];//全部客户
            $data['message'] = '请求错误';
            return $this->autoAjax($data);
        }
        /**
         * 客户首页统计
         * @return mixed
         */
        public function all()
        {
            $data['code'] = 0;
            $data['data']['all'] = 111;//全部客户
            $data['data']['toDayKefu'] = 60;//今日客户
            $data['data']['month'] = 100;//本月客户
            $data['data']['toDayTourist'] = 10;//今日游客
            return $this->autoAjax($data);
        }
        /**
         * @param string $time
         * @return array
         * @throws DataNotFoundException
         * @throws ModelNotFoundException
         * @throws \think\db\exception\DbException
         */
        public function getKefuMobileStatistics(string $time)
        {
            $res = [
                'tourist' => $this->dao->getKefuMobileStatisticsList($time, 1),
                'list' => $this->dao->getKefuMobileStatisticsList($time, 0),
            ];

            foreach ($res['tourist'] as &$item) {
                if (strstr($item['time'], '-') !== false) {
                    $item['time'] = explode('-', $item['time']);
                    $item['time'] = $item['time'][count($item['time']) - 1];
                }
            }
            foreach ($res['list'] as &$item) {
                if (strstr($item['time'], '-') !== false) {
                    $item['time'] = explode('-', $item['time']);
                    $item['time'] = $item['time'][count($item['time']) - 1];
                }
            }

            return $res;
        }
		/**
		 * 删除地址
		 */
		public function delete()
		{
			if ($this->request->isPost()) {
				$address = $this->user->addresses()->where('id', $this->request->post('id'))->find();
				if ($address) {
					if ($address['is_default'] == 1) {
						// 删除了默认地址
						$address->is_default = 0;
						$this->user->addresses()->where('id', '<>', $this->request->post('id'))->limit(1)->setField('is_default', 1);
					}
					$address->status = -2;
					if ($address->save()) {
						// 其他改为非默认
						$data['code'] = 0;
						$data['message'] = '删除成功';
					} else {
						$data['code'] = 1;
						$data['message'] = '删除异常';
					}
				} else {
					$data['code'] = 1;
					$data['message'] = '未发现当前地址';
				}
			} else {
				$data['code'] = 1;
				$data['message'] = '请求错误';
			}
			return $this->autoAjax($data);
		}

		public function setDefault()
		{
			if ($this->request->isPost()) {
				$address = $this->user->addresses()->where('id', $this->request->post('id'))->find();
				if ($address && 0 == $address['is_default']) {
					$address['is_default'] = 1;
					if ($address->save()) {
						// 其他改为非默认
						$this->user->addresses()->where('id', '<>', $this->request->post('id'))->setField('is_default', 0);
						$data['code'] = 0;
						$data['message'] = '修改成功';
					} else {
						$data['code'] = 1;
						$data['message'] = '修改异常';
					}
				} else {
					$data['code'] = 1;
					$data['message'] = '未更新';
				}
			} else {
				$data['code'] = 1;
				$data['message'] = '请求错误';
			}
			return $this->autoAjax($data);
		}

		/**
		 * 我的所有地址
		 */
		public function gets()
		{
			$address = $this->user->addresses()->where('status', 1)->select();
			$data['data'] = $address;
			$data['code'] = 0;
			return $this->autoAjax($data);
		}

		/**
		 * 地域信息
		 */
		public function region()
		{
			$address =  Region::where('status',1)->select();
			$data['data'] = $address;
			$data['code'] = 0;
			return $this->autoAjax($data);
		}

		/**
		 * 我的地址
		 */
		public function get()
		{
			if ($this->request->isPost()) {
				;
			} else {
				if ($this->request->has('id', 'get') && $this->request->get('id')) {
					$address = $this->user->addresses()->where('id', $this->request->get('id'))->where('status', 1)->find();
				} else {
					$address = $this->user->addresses()->where('is_default', 1)->where('status', 1)->find();
				}
				if ($address) {
					$data['code'] = 0;
					$data['data'] = $address;
				} else {
					$data['code'] = 1;
					$data['message'] = '地址不存在';
				}
			}
			return $this->autoAjax($data);
		}

		/**
		 * 更新/添加地址
		 */
		public function update()
		{
			if ($this->request->isPost()) {
				$validate = $this->validate($this->request->post(), 'app\store\validate\Address');
				if (true === $validate) {
					$event = new AddressEvent();
					$data = $event->update($this->request->post(), $this->user);
				} else {
					$data['code'] = 1;
					$data['message'] = $validate;
				}
			} else {
				$data['code'] = 1;
				$data['message'] = '请求错误';
			}
			return $this->autoAjax($data);
		}
	}
